import { createStore } from '@/airstore';
import ApiClient from '@/services/apiClient';
import { transPlanStatus } from './components/taskVFFormat';

function transformTask(task) {
  return {
    airplane_no: task.TS_SCAcno,
    areaShow: task.TS_SCTaskPaperTypeShow,
    area: task.TS_SCTaskPaperType,
    taskCount: task.TS_SCTaskCount,
    packNo: task.TS_SCPackNO,
    station: task.TS_SCTaskStation,
    areaPer: task.TS_SCUser_Name,
    planDate: task.TS_SCDate,
    finishTaskCount: task.TS_SCFinishTaskCount,
    taskPersent: task.TS_SCTaskPersent,
    id: task.TS_TaskStageTid,
    flightTid: task.TS_SCFlightTid,
  };
}
function transformJobObj(spo) {
  return {
    id: spo.Tid,
    jobCard: spo.MpDp_JobCard,
    planDate: spo.MpDp_PlanDate,
    acno: spo.MpDp_Acno,
    packNo: spo.MpDp_WorkOrder,
    jcTitle: spo.MpDp_CardTitle,
    worker: spo.Name,
    finishDate: spo.MpDp_Date,
    planStatusDes: transPlanStatus(spo.MpDp_PlanStatus),
    taskStageTid: spo.MpDp_TaskStageTid,
    flightTid: spo.MpDp_FlightTid,
    area: spo.MpDpCardArea,
  };
}

export default createStore({
  namespace: 'AreaJob',
  filterCond: {
    startDate: null,
    endDate: null,
    selAirport: '',
    selAirplane: '',
  },
  list: [],
  jobDetail: [],
  setJobList(jbData, filter) {
    if (jbData) {
      this.list = jbData.map(transformTask);
    } else {
      this.list = [];
    }
    this.filterCond = filter;
  },
  setJobDetail(jbdData) {
    this.jobDetail = jbdData.map(transformJobObj);
  },
  async getJobList(loginName, filter) {
    const searchPara = {
      datefrom: filter.startDate.format('YYYY-MM-DD'),
      dateto: filter.endDate.format('YYYY-MM-DD'),
      stationid: filter.selAirport,
      acno: filter.selAirplane,
    };
    const srRes = await ApiClient.post('mpController/v1/getScArea', {
      loginUser: loginName,
      jsonResult: JSON.stringify(searchPara),
    });
    this.setJobList(!srRes.errorCode ? srRes.data : null, filter);
    return srRes;
  },
  async getAreaJobDetail(loginName, searchPara) {
    const srRes = await ApiClient.post('mpController/v1/getAreaTaskDetail', {
      loginUser: loginName,
      jsonResult: JSON.stringify(searchPara),
    });
    this.setJobDetail(srRes.data || []);
    return srRes;
  },
});
